home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / tar-dist / port.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-26  |  25.6 KB  |  1,255 lines

  1. /* Supporting routines which may sometimes be missing.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * @(#)port.c 1.15    87/11/05    by John Gilmore, 1986
  22.  *
  23.  * These are routines not available in all environments.
  24.  *
  25.  * I know this introduces an extra level of subroutine calls and is
  26.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  27.  * Missed-Em Vee losers suffer a little.  This software is proud to
  28.  * have been written on a BSD system.
  29.  */
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <signal.h>
  34. #include <errno.h>
  35.  
  36. #ifdef    MSDOS
  37. #include <fcntl.h>
  38. #else
  39. #include <sys/file.h>
  40. #endif
  41.  
  42. #include "tar.h"
  43. #include "port.h"
  44.  
  45. #ifdef USG
  46. #include <string.h>
  47. #else
  48. extern size_t strlen();
  49. #endif
  50.  
  51. extern long baserec;
  52. /*
  53.  * Some people (e.g. V7) don't have a #define for these.
  54.  */
  55. #ifndef    O_BINARY
  56. #define    O_BINARY    0
  57. #endif
  58. #ifndef    O_RDONLY
  59. #define    O_RDONLY    0
  60. #endif
  61. #ifndef NULL
  62. #define NULL 0
  63. #endif
  64.  
  65. /* JF: modified so all configuration information can appear here, instead of
  66.    being scattered through the file.  Add all the machine-dependent #ifdefs
  67.    here */
  68. #undef WANT_DUMB_GETDATE/* WANT_DUMB_GETDATE --> getdate() */
  69. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  70. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  71. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  72. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  73.             /* EMUL_OPEN3 --> open3() */
  74. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  75. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  76.                merge_sort() quote_copy_string() un_quote_string() */
  77. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  78. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  79. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  80. #undef WANT_FTRUNCATE    /* WANT_FRTUNCATE --> frtruncate() */
  81.  
  82. /* Define only ONE of these four . . . */
  83. #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  84.                no varargs support */
  85. #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  86.                vfprintf() */
  87. #undef STDC_MSG        /* Define this one if you are using ANSI C and
  88.                and have vfprintf() */
  89. #undef LOSING_MSG    /* Define this one if you don't have any of the
  90.                above */
  91. #ifdef USG
  92. #define WANT_STRING
  93. #define WANT_VALLOC
  94.  
  95. #if defined(sgi) && defined(mips)
  96. #define WANG_GETWD
  97. #endif
  98.  
  99. #endif
  100.  
  101. #ifdef MINIX
  102. #define WANT_BZERO
  103. #endif
  104.  
  105. #ifdef MSDOS
  106. #define WANT_STRING
  107. #define WANT_MKNOD
  108. #define WANT_UTILS
  109. #define WANT_VALLOC
  110. char TTY_NAME[] = "con";
  111. #else /* not MSDOS */
  112. char TTY_NAME[] ="/dev/tty";
  113.  
  114. #ifdef BSD42
  115. /* BSD systems should do this even if __STDC__, because
  116.    we might be using an ANSI compiler without an ANSI library.  */
  117. #define DOPRNT_MSG
  118. #else /* not BSD */
  119. #ifdef __STDC__
  120. #define STDC_MSG
  121. #else /* not ANSI C */
  122. #define LOSING_MSG
  123. #endif /* not ANSI C */
  124. #endif /* not BSD */
  125.  
  126. #define WANT_UTILS
  127. #define WANT_CK_PIPE
  128. #define WANT_STRSTR
  129. #endif /* not MSDOS */
  130.  
  131. /* End of system-dependent #ifdefs */
  132.  
  133. #ifdef WANT_DUMB_GETDATE
  134. /* JF a getdate() routine takes a date/time/etc and turns it into a time_t */
  135. /* This one is a quick hack I wrote in about five minutes to see if the N
  136.    option works.  Someone should replace it with one that works */
  137.  
  138. /* This getdate takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  139.    into a time_t .  Its not well tested or anything. . .  */
  140. /* In general, you should use the getdate() supplied in getdate.y */
  141.  
  142. #define OFF_FROM GMT 18000        /* Change for your time zone! */
  143.  
  144. time_t
  145. getdate(str)
  146. char *str;
  147. {
  148.     int month,day,year,hour,minute,second;
  149.     time_t ret;
  150.     int    n;
  151.  
  152. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  153. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  154.  
  155. #define SECS_PER_DAY (24L*60*60)
  156.     static int days_per_month[2][12] = {
  157.         31,28,31,30,31,30,31,31,30,31,30,31,
  158.         31,29,31,30,31,30,31,31,30,31,30,31
  159.     };
  160.  
  161.     static int days_per_year[2]={365,366};
  162.  
  163.     month=day=year=hour=minute=second=0;
  164.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  165.     if(n<3)
  166.         return 0;
  167.     if(year<100)
  168.         year+=1900;
  169.     if(year<1970)
  170.         return 0;
  171.     ret=0;
  172.  
  173.     ret+=OFF_FROM_GMT;
  174.  
  175.     for(n=1970;n<year;n++)
  176.         if(n%4==0 && n%400!=0)
  177.             ret+=SECS_PER_LEAP_YEAR;
  178.         else
  179.             ret+=SECS_PER_YEAR;
  180.  
  181.     month--;
  182.     for(n=0;n<month;n++) {
  183.         if(year%4==0 && year%400!=0)
  184.             ret+=SECS_PER_DAY*days_per_month[1][n];
  185.         else
  186.             ret+=SECS_PER_DAY*days_per_month[0][n];
  187.     }
  188.     ret+=SECS_PER_DAY*(day-1);
  189.     ret+=second+minute*60+hour*60*60;
  190.     return ret;
  191. }
  192. #endif
  193.  
  194. #ifdef WANT_VALLOC
  195. /*
  196.  * valloc() does a malloc() on a page boundary.  On some systems,
  197.  * this can make large block I/O more efficient.
  198.  */
  199. char *
  200. valloc (size)
  201.     unsigned size;
  202. {
  203.     extern char *malloc ();
  204.     return (malloc (size));
  205. }
  206. #endif
  207. /*
  208.  *                NMKDIR.C
  209.  *
  210.  * Written by Robert Rother, Mariah Corporation, August 1985. 
  211.  *
  212.  * I wrote this out of shear disgust with myself because I couldn't
  213.  * figure out how to do this in /bin/sh.
  214.  *
  215.  * If you want it, it's yours.  All I ask in return is that if you
  216.  * figure out how to do this in a Bourne Shell script you send me
  217.  * a copy.
  218.  *                    sdcsvax!rmr or rmr@uscd
  219. *
  220. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  221. * subroutine.    11Mar86; hoptoad!gnu
  222. *
  223. * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  224. * subroutine didn't return EEXIST.  It does now.
  225. */
  226.  
  227. /*
  228.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  229.  */
  230. #ifdef WANT_MKDIR
  231. int
  232. mkdir(dpath, dmode)
  233.     char *dpath;
  234.     int dmode;
  235. {
  236.     int cpid, status;
  237.     struct stat statbuf;
  238.     extern int errno;
  239.  
  240.     if (stat(dpath,&statbuf) == 0) {
  241.         errno = EEXIST;        /* Stat worked, so it already exists */
  242.         return -1;
  243.     }
  244.  
  245.     /* If stat fails for a reason other than non-existence, return error */
  246.     if (errno != ENOENT) return -1; 
  247.  
  248.     switch (cpid = fork()) {
  249.  
  250.     case -1:            /* Error in fork() */
  251.         return(-1);        /* Errno is set already */
  252.  
  253.     case 0:                /* Child process */
  254.         /*
  255.          * Cheap hack to set mode of new directory.  Since this
  256.          * child process is going away anyway, we zap its umask.
  257.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  258.          * directory.  Does anybody care?
  259.          */
  260.         status = umask(0);    /* Get current umask */
  261.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  262.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  263.         _exit(-1);        /* Can't exec /bin/mkdir */
  264.     
  265.     default:            /* Parent process */
  266.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  267.     }
  268.  
  269.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  270.         errno = EIO;        /* We don't know why, but */
  271.         return -1;        /* /bin/mkdir failed */
  272.     }
  273.  
  274.     return 0;
  275. }
  276. int
  277. rmdir(dpath)
  278.     char *dpath;
  279. {
  280.     int cpid, status;
  281.     struct stat statbuf;
  282.     extern int errno;
  283.  
  284.     if (stat(dpath,&statbuf) != 0) {
  285.         /* Stat just set errno.  We don't have to */
  286.         return -1;
  287.     }
  288.  
  289.     switch (cpid = fork()) {
  290.  
  291.     case -1:            /* Error in fork() */
  292.         return(-1);        /* Errno is set already */
  293.  
  294.     case 0:                /* Child process */
  295.         execl("/bin/rmdir", "rmdir", dpath, (char *)0);
  296.         _exit(-1);        /* Can't exec /bin/mkdir */
  297.     
  298.     default:            /* Parent process */
  299.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  300.     }
  301.  
  302.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  303.         errno = EIO;        /* We don't know why, but */
  304.         return -1;        /* /bin/mkdir failed */
  305.     }
  306.  
  307.     return 0;
  308. }
  309. #endif
  310.  
  311. #ifdef WANT_STRING
  312. /*
  313.  * Translate V7 style into Sys V style.
  314.  */
  315. #include <string.h>
  316. #include <memory.h>
  317.  
  318. char *
  319. index (s, c)
  320.     char *s;
  321.     int c;
  322. {
  323.     return (strchr (s, c));
  324. }
  325.  
  326. char *
  327. rindex(s,c)
  328. char *s;
  329. int c;
  330. {
  331.     return strrchr(s,c);
  332. }
  333.  
  334. void
  335. bcopy (s1, s2, n)
  336.     char *s1, *s2;
  337.     int n;
  338. {
  339.     (void) memcpy (s2, s1, n);
  340. }
  341.  
  342. void
  343. bzero (s1, n)
  344.     char *s1;
  345.     int n;
  346. {
  347.     (void) memset(s1, 0, n);
  348. }
  349.  
  350. int
  351. bcmp(s1, s2, n)
  352.     char    *s1, *s2;
  353.     int    n;
  354. {
  355.     return memcmp(s1, s2, n);
  356. }
  357. #endif
  358.  
  359. #ifdef WANT_BZERO
  360. /* Minix has bcopy but not bzero, and no memset.  Thanks, Andy. */
  361. void
  362. bzero (s1, n)
  363.     register char *s1;
  364.     register int n;
  365. {
  366.     while (n--) *s1++ = '\0';
  367. }
  368.  
  369. /* It also has no bcmp() */
  370. int
  371. bcmp (s1, s2, n) 
  372.     register char *s1,*s2;
  373.     register int n;
  374. {
  375.     for ( ; n-- ; ++s1, ++s2) {
  376.         if (*s1 != *s2) return *s1 - *s2;
  377.     }
  378.     return 0;
  379. }
  380.  
  381. /*
  382.  * Groan, Minix doesn't have execlp either!
  383.  *
  384.  * execlp(file,arg0,arg1...argn,(char *)NULL)
  385.  * exec a program, automatically searching for the program through
  386.  * all the directories on the PATH.
  387.  *
  388.  * This version is naive about variable argument lists, it assumes
  389.  * a straightforward C calling sequence.  If your system has odd stacks
  390.  * *and* doesn't have execlp, YOU get to fix it.
  391.  */
  392. int
  393. execlp(filename, arg0)
  394.     char *filename, *arg0;
  395. {
  396.     register char *p, *path;    
  397.     register char *fnbuffer;
  398.     char **argstart = &arg0;
  399.     struct stat statbuf;
  400.     extern char **environ;
  401.     extern int errno;
  402.     extern char *malloc(), *getenv(), *index();
  403.  
  404.     if ((p = getenv("PATH")) == NULL) {
  405.         /* couldn't find path variable -- try to exec given filename */
  406.         return execve(filename, argstart, environ);
  407.     }
  408.  
  409.     /*
  410.      * make a place to build the filename.  We malloc larger than we
  411.      * need, but we know it will fit in this.
  412.      */
  413.     fnbuffer = malloc( strlen(p) + 1 + strlen(filename) );
  414.     if (fnbuffer == NULL) {
  415.         errno = ENOMEM;
  416.         return -1;
  417.     }
  418.  
  419.     /*
  420.      * try each component of the path to see if the file's there
  421.      * and executable.
  422.      */
  423.     for (path = p ; path ; path = p) {
  424.         /* construct full path name to try */
  425.         if ((p = index(path,':')) == NULL) {
  426.             strcpy(fnbuffer, path);
  427.         } else {
  428.             strncpy(fnbuffer, path, p-path);
  429.             fnbuffer[p-path] = '\0';
  430.             p++;        /* Skip : for next time */
  431.         }
  432.         if (strlen(fnbuffer) != 0)
  433.             strcat(fnbuffer,"/");
  434.         strcat(fnbuffer,filename);
  435.  
  436.         /* check to see if file is there and is a normal file */
  437.         if (stat(fnbuffer, &statbuf) < 0) {
  438.             if (errno == ENOENT)
  439.                 continue; /* file not there,keep on looking */
  440.             else
  441.                 goto fail; /* failed for some reason, return */
  442.         }
  443.         if ( (statbuf.st_mode & S_IFMT) != S_IFREG) continue;
  444.  
  445.         if (execve(fnbuffer, argstart, environ) < 0
  446.             && errno != ENOENT
  447.             && errno != ENOEXEC) {
  448.             /* failed, for some other reason besides "file
  449.              * not found" or "not a.out format"
  450.              */
  451.             goto fail;
  452.         }
  453.  
  454.         /*
  455.          * If we got error ENOEXEC, the file is executable but is
  456.          * not an object file.  Try to execute it as a shell script,
  457.          * returning error if we can't execute /bin/sh.
  458.          *
  459.          * FIXME, this code is broken in several ways.  Shell
  460.          * scripts should not in general be executed by the user's
  461.          * SHELL variable program.  On more mature systems, the
  462.          * script can specify with #!/bin/whatever.  Also, this
  463.          * code clobbers argstart[-1] if the exec of the shell
  464.          * fails.
  465.          */
  466.         if (errno == ENOEXEC) {
  467.             char *shell;
  468.  
  469.             /* Try to execute command "sh arg0 arg1 ..." */
  470.             if ((shell = getenv("SHELL")) == NULL)
  471.                 shell = "/bin/sh";
  472.             argstart[-1] = shell;
  473.             argstart[0] = fnbuffer;
  474.             execve(shell, &argstart[-1], environ);
  475.             goto fail;    /* Exec didn't work */
  476.         }
  477.  
  478.         /* 
  479.          * If we succeeded, the execve() doesn't return, so we
  480.          * can only be here is if the file hasn't been found yet.
  481.          * Try the next place on the path.
  482.          */
  483.     }
  484.  
  485.     /* all attempts failed to locate the file.  Give up. */
  486.     errno = ENOENT;
  487.  
  488. fail:
  489.     free(fnbuffer);
  490.     return -1;
  491. }
  492. #endif
  493.  
  494.  
  495. #ifdef EMUL_OPEN3
  496. #include "open3.h"
  497. /*
  498.  * open3 -- routine to emulate the 3-argument open system
  499.  * call that is present in most modern Unix systems.
  500.  * This version attempts to support all the flag bits except for O_NDELAY
  501.  * and O_APPEND, which are silently ignored.  The emulation is not as efficient
  502.  * as the real thing (at worst, 4 system calls instead of one), but there's
  503.  * not much I can do about that.
  504.  *
  505.  * Written 6/10/87 by rmtodd@uokmax
  506.  *
  507.  * open3(path, flag, mode)
  508.  * Attempts to open the file specified by
  509.  * the given pathname.  The following flag bits (#defined in tar.h)
  510.  * specify options to the routine:
  511.  *    O_RDONLY    file open for read only
  512.  *    O_WRONLY    file open for write only
  513.  *    O_RDWR        file open for both read & write
  514.  * (Needless to say, you should only specify one of the above).
  515.  *     O_CREAT        file is created with specified mode if it needs to be.
  516.  *    O_TRUNC        if file exists, it is truncated to 0 bytes
  517.  *    O_EXCL        used with O_CREAT--routine returns error if file exists
  518.  * Function returns file descriptor if successful, -1 and errno if not.
  519.  */
  520.  
  521. /*
  522.  * array to give arguments to access for various modes
  523.  * FIXME, this table depends on the specific integer values of O_XXX,
  524.  * and also contains integers (args to 'access') that should be #define's.
  525.  */
  526. static int modes[] =
  527.     {
  528.         04, /* O_RDONLY */
  529.         02, /* O_WRONLY */
  530.         06, /* O_RDWR */
  531.         06, /* invalid but we'd better cope -- O_WRONLY+O_RDWR */
  532.     };
  533.  
  534. /* Shut off the automatic emulation of open(), we'll need it. */
  535. #undef open
  536.  
  537. int
  538. open3(path, flags, mode)
  539. char *path;
  540. int flags, mode;
  541. {
  542.     extern int errno;
  543.     int exists = 1;
  544.     int call_creat = 0;
  545.     int fd;
  546.     /*
  547.      * We actually do the work by calling the open() or creat() system
  548.      * call, depending on the flags.  Call_creat is true if we will use 
  549.      * creat(), false if we will use open().
  550.      */
  551.  
  552.     /*
  553.      * See if the file exists and is accessible in the requested mode. 
  554.      *
  555.      * Strictly speaking we shouldn't be using access, since access checks
  556.      * against real uid, and the open call should check against euid.
  557.      * Most cases real uid == euid, so it won't matter.   FIXME.
  558.      * FIXME, the construction "flags & 3" and the modes table depends
  559.      * on the specific integer values of the O_XXX #define's.  Foo!
  560.      */
  561.     if (access(path,modes[flags & 3]) < 0) {
  562.         if (errno == ENOENT) {
  563.             /* the file does not exist */
  564.             exists = 0;
  565.         } else {
  566.             /* probably permission violation */
  567.             if (flags & O_EXCL) {
  568.                 /* Oops, the file exists, we didn't want it. */
  569.                 /* No matter what the error, claim EEXIST. */
  570.                 errno = EEXIST;
  571.             }
  572.             return -1;
  573.         }
  574.     }
  575.  
  576.     /* if we have the O_CREAT bit set, check for O_EXCL */
  577.     if (flags & O_CREAT) {
  578.         if ((flags & O_EXCL) && exists) {
  579.             /* Oops, the file exists and we didn't want it to. */
  580.             errno = EEXIST;
  581.             return -1;
  582.         }
  583.         /*
  584.          * If the file doesn't exist, be sure to call creat() so that
  585.          * it will be created with the proper mode.
  586.          */
  587.         if (!exists) call_creat = 1;
  588.     } else {
  589.         /* If O_CREAT isn't set and the file doesn't exist, error. */
  590.         if (!exists) {
  591.             errno = ENOENT;
  592.             return -1;
  593.         }
  594.     }
  595.  
  596.     /*
  597.      * If the O_TRUNC flag is set and the file exists, we want to call
  598.      * creat() anyway, since creat() guarantees that the file will be
  599.      * truncated and open()-for-writing doesn't.
  600.      * (If the file doesn't exist, we're calling creat() anyway and the
  601.      * file will be created with zero length.)
  602.      */
  603.     if ((flags & O_TRUNC) && exists) call_creat = 1;
  604.     /* actually do the call */
  605.     if (call_creat) {
  606.         /*
  607.          * call creat.  May have to close and reopen the file if we
  608.          * want O_RDONLY or O_RDWR access -- creat() only gives
  609.          * O_WRONLY.
  610.          */
  611.         fd = creat(path,mode);
  612.         if (fd < 0 || (flags & O_WRONLY)) return fd;
  613.         if (close(fd) < 0) return -1;
  614.         /* Fall out to reopen the file we've created */
  615.     }
  616.  
  617.     /*
  618.      * calling old open, we strip most of the new flags just in case.
  619.      */
  620.     return open(path, flags & (O_RDONLY|O_WRONLY|O_RDWR|O_BINARY));
  621. }
  622. #endif
  623.  
  624. #ifdef    WANT_MKNOD
  625. /* Fake mknod by complaining */
  626. int
  627. mknod(path, mode, dev)
  628.     char        *path;
  629.     unsigned short    mode;
  630.     dev_t        dev;
  631. {
  632.     extern int    errno;
  633.     int        fd;
  634.     
  635.     errno = ENXIO;        /* No such device or address */
  636.     return -1;        /* Just give an error */
  637. }
  638.  
  639. /* Fake links by copying */
  640. int
  641. link(path1, path2)
  642.     char        *path1;
  643.     char        *path2;
  644. {
  645.     char    buf[256];
  646.     int    ifd, ofd;
  647.     int    nrbytes;
  648.     int    nwbytes;
  649.  
  650.     fprintf(stderr, "%s: %s: cannot link to %s, copying instead\n",
  651.         tar, path1, path2);
  652.     if ((ifd = open(path1, O_RDONLY|O_BINARY)) < 0)
  653.         return -1;
  654.     if ((ofd = creat(path2, 0666)) < 0)
  655.         return -1;
  656.     setmode(ofd, O_BINARY);
  657.     while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
  658.         if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
  659.             nrbytes = -1;
  660.             break;
  661.         }
  662.     }
  663.     /* Note use of "|" rather than "||" below: we want to close
  664.      * the files even if an error occurs.
  665.      */
  666.     if ((nrbytes < 0) | (0 != close(ifd)) | (0 != close(ofd))) {
  667.         unlink(path2);
  668.         return -1;
  669.     }
  670.     return 0;
  671. }
  672.  
  673. /* everyone owns everything on MS-DOS (or is it no one owns anything?) */
  674. int
  675. chown(path, uid, gid)
  676.     char    *path;
  677.     int    uid;
  678.     int    gid;
  679. {
  680.     return 0;
  681. }
  682.  
  683. int
  684. geteuid()
  685. {
  686.     return 0;
  687. }
  688. #endif    /* WANT_MKNOD */
  689.  
  690. #ifdef WANT_UTILS
  691. /* Stash argv[0] here so panic will know what the program is called */
  692. char *myname = 0;
  693.  
  694. void
  695. panic(s)
  696. char *s;
  697. {
  698.     if(myname)
  699.         fprintf(stderr,"%s:",myname);
  700.     fprintf(stderr,s);
  701.     putc('\n',stderr);
  702.     exit(12);
  703. }
  704.  
  705. char *
  706. ck_realloc(ptr,size)
  707. char *ptr;
  708. size_t size;
  709. {
  710.     char *ret;
  711.     char *realloc();
  712.  
  713.     ret=realloc(ptr,size);
  714.     if(ret==0)
  715.         panic("Couldn't re-allocate memory");
  716.     return ret;
  717. }
  718.  
  719.  
  720. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  721.    nor do we care, as long as it doesn't mind being aligned on a char boundry.
  722.  */
  723.  
  724. struct buffer {
  725.     int    allocated;
  726.     int    length;
  727.     char    *b;
  728. };
  729.  
  730. #define MIN_ALLOCATE 50
  731.  
  732. char *
  733. init_buffer()
  734. {
  735.     struct buffer *b;
  736.     char *ck_malloc();
  737.  
  738.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  739.     b->allocated=MIN_ALLOCATE;
  740.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  741.     b->length=0;
  742.     return (char *)b;
  743. }
  744.  
  745. void
  746. flush_buffer(bb)
  747. char *bb;
  748. {
  749.     struct buffer *b;
  750.  
  751.     b=(struct buffer *)bb;
  752.     free(b->b);
  753.     b->b=0;
  754.     b->allocated=0;
  755.     b->length=0;
  756.     free((void *)b);
  757. }
  758.  
  759. void
  760. add_buffer(bb,p,n)
  761. char *bb;
  762. char *p;
  763. int n;
  764. {
  765.     struct buffer *b;
  766.  
  767.     b=(struct buffer *)bb;
  768.     if(b->length+n>b->allocated) {
  769.         b->allocated*=2;
  770.         b->b=(char *)ck_realloc(b->b,b->allocated);
  771.     }
  772.     bcopy(p,b->b+b->length,n);
  773.     b->length+=n;
  774. }
  775.  
  776. char *
  777. get_buffer(bb)
  778. char *bb;
  779. {
  780.     struct buffer *b;
  781.  
  782.     b=(struct buffer *)bb;
  783.     return b->b;
  784. }
  785.  
  786. char *
  787. merge_sort(list,n,off,cmp)
  788. char *list;
  789. int (*cmp)();
  790. unsigned n;
  791. int off;
  792. {
  793.     char *ret;
  794.  
  795.     char *alist,*blist;
  796.     unsigned alength,blength;
  797.  
  798.     char *tptr;
  799.     int tmp;
  800.     char **prev;
  801. #define NEXTOF(ptr)    (* ((char **)(((char *)(ptr))+off) ) )
  802.     if(n==1)
  803.         return list;
  804.     if(n==2) {
  805.         if((*cmp)(list,NEXTOF(list))>0) {
  806.             ret=NEXTOF(list);
  807.             NEXTOF(ret)=list;
  808.             NEXTOF(list)=0;
  809.             return ret;
  810.         }
  811.         return list;
  812.     }
  813.     alist=list;
  814.     alength=(n+1)/2;
  815.     blength=n/2;
  816.     for(tptr=list,tmp=(n-1)/2;tmp;tptr=NEXTOF(tptr),tmp--)
  817.         ;
  818.     blist=NEXTOF(tptr);
  819.     NEXTOF(tptr)=0;
  820.  
  821.     alist=merge_sort(alist,alength,off,cmp);
  822.     blist=merge_sort(blist,blength,off,cmp);
  823.     prev = &ret;
  824.     for(;alist && blist;) {
  825.         if((*cmp)(alist,blist)<0) {
  826.             tptr=NEXTOF(alist);
  827.             *prev = alist;
  828.             prev = &(NEXTOF(alist));
  829.             alist=tptr;
  830.         } else {
  831.             tptr=NEXTOF(blist);
  832.             *prev = blist;
  833.             prev = &(NEXTOF(blist));
  834.             blist=tptr;
  835.         }
  836.     }
  837.     if(alist)
  838.         *prev = alist;
  839.     else
  840.         *prev = blist;
  841.  
  842.     return ret;
  843. }
  844.  
  845. char *
  846. ck_malloc(size)
  847. size_t size;
  848. {
  849.     char *ret;
  850.     char *malloc();
  851.  
  852.     ret=malloc(size);
  853.     if(ret==0)
  854.         panic("Couldn't allocate memory");
  855.     return ret;
  856. }
  857.  
  858. void
  859. ck_close(fd)
  860. int fd;
  861. {
  862.     if(close(fd)<0) {
  863.         msg_perror("can't close a file #%d",fd);
  864.         exit(EX_SYSTEM);
  865.     }
  866. }
  867.  
  868. #include <ctype.h>
  869.  
  870. /* Quote_copy_string is like quote_string, but instead of modifying the
  871.    string in place, it malloc-s a copy  of the string, and returns that.
  872.    If the string does not have to be quoted, it returns the NULL string.
  873.    The allocated copy can, of course, be freed with free() after the
  874.    caller is done with it.
  875.  */
  876. char *
  877. quote_copy_string(string)
  878. char *string;
  879. {
  880.     char    *from_here;
  881.     char    *to_there = 0;
  882.     char    *copy_buf = 0;
  883.     int    c;
  884.     int    copying = 0;
  885.  
  886.     from_here=string;
  887.     while(*from_here) {
  888.         c= *from_here++;
  889.         if(c=='\\') {
  890.             if(!copying) {
  891.                 int n;
  892.  
  893.                 n=(from_here-string)-1;
  894.                 copying++;
  895.                 copy_buf=(char *)malloc(n+3+strlen(from_here)*4);
  896.                 if(!copy_buf)
  897.                     return 0;
  898.                 bcopy(string,copy_buf,n);
  899.                 to_there=copy_buf+n;
  900.             }
  901.             *to_there++='\\';
  902.             *to_there++='\\';
  903.         } else if(isprint(c)) {
  904.             if(copying)
  905.                 *to_there++= c;
  906.         } else {
  907.             if(!copying) {
  908.                 int    n;
  909.  
  910.                 n=(from_here-string)-1;
  911.                 copying++;
  912.                 copy_buf=(char *)malloc(n+3+strlen(from_here)*4);
  913.                 if(!copy_buf)
  914.                     return 0;
  915.                 bcopy(string,copy_buf,n);
  916.                 to_there=copy_buf+n;
  917.             }
  918.             *to_there++='\\';
  919.             if(c=='\n') *to_there++='n';
  920.             else if(c=='\t') *to_there++='t';
  921.             else if(c=='\f') *to_there++='f';
  922.             else if(c=='\b') *to_there++='b';
  923.             else if(c=='\r') *to_there++='r';
  924.             else if(c=='\177') *to_there++='?';
  925.             else {
  926.                 to_there[0]=(c>>6)+'0';
  927.                 to_there[1]=((c>>3)&07)+'0';
  928.                 to_there[2]=(c&07)+'0';
  929.                 to_there+=3;
  930.             }
  931.         }
  932.     }
  933.     if(copying) {
  934.         *to_there='\0';
  935.         return copy_buf;
  936.     }
  937.     return (char *)0;
  938. }
  939.  
  940.  
  941. /* Un_quote_string takes a quoted c-string (like those produced by
  942.    quote_string or quote_copy_string and turns it back into the
  943.    un-quoted original.  This is done in place.
  944.  */
  945.  
  946. /* There is no un-quote-copy-string.  Write it yourself */
  947.  
  948. char *un_quote_string(string)
  949. char *string;
  950. {
  951.     char *ret;
  952.     char *from_here;
  953.     char *to_there;
  954.     int    tmp;
  955.  
  956.     ret=string;
  957.     to_there=string;
  958.     from_here=string;
  959.     while(*from_here) {
  960.         if(*from_here!='\\') {
  961.             if(from_here!=to_there)
  962.                 *to_there++= *from_here++;
  963.             else
  964.                 from_here++,to_there++;
  965.             continue;
  966.         }
  967.         switch(*++from_here) {
  968.         case '\\':
  969.             *to_there++= *from_here++;
  970.             break;
  971.         case 'n':
  972.             *to_there++= '\n';
  973.             from_here++;
  974.             break;
  975.         case 't':
  976.             *to_there++= '\t';
  977.             from_here++;
  978.             break;
  979.         case 'f':
  980.             *to_there++= '\f';
  981.             from_here++;
  982.             break;
  983.         case 'b':
  984.             *to_there++= '\b';
  985.             from_here++;
  986.             break;
  987.         case 'r':
  988.             *to_there++= '\r';
  989.             from_here++;
  990.             break;
  991.         case '?':
  992.             *to_there++= 0177;
  993.             from_here++;
  994.             break;
  995.         case '0':
  996.         case '1':
  997.         case '2':
  998.         case '3':
  999.         case '4':
  1000.         case '5':
  1001.         case '6':
  1002.         case '7':
  1003.             tmp= *from_here - '0';
  1004.             from_here++;
  1005.             if(*from_here<'0' || *from_here>'7') {
  1006.                 *to_there++= tmp;
  1007.                 break;
  1008.             }
  1009.             tmp= tmp*8 + *from_here-'0';
  1010.             from_here++;
  1011.             if(*from_here<'0' || *from_here>'7') {
  1012.                 *to_there++= tmp;
  1013.                 break;
  1014.             }
  1015.             tmp=tmp*8 + *from_here-'0';
  1016.             from_here++;
  1017.             *to_there=tmp;
  1018.             break;
  1019.         default:
  1020.             ret=0;
  1021.             *to_there++='\\';
  1022.             *to_there++= *from_here++;
  1023.             break;
  1024.         }
  1025.     }
  1026.     *to_there++='\0';
  1027.     return ret;
  1028. }
  1029. #endif
  1030.  
  1031. #ifdef WANT_CK_PIPE
  1032. void ck_pipe(pipes)
  1033. int *pipes;
  1034. {
  1035.     if(pipe(pipes)<0) {
  1036.         msg_perror("can't open a pipe");
  1037.         exit(EX_SYSTEM);
  1038.     }
  1039. }
  1040.  
  1041. #endif
  1042.  
  1043. #ifdef WANT_GETWD
  1044. char *
  1045. getwd(path)
  1046. char *path;
  1047. {
  1048.     FILE *fp;
  1049.     FILE *popen();
  1050.  
  1051.     fp=popen("pwd","r");
  1052.     if(!fp)
  1053.         return 0;
  1054.     if(!fgets(path,100,fp))
  1055.         return 0;
  1056.     if(!pclose(fp))
  1057.         return 0;
  1058.     return path;
  1059. }
  1060. #endif /* WANT_CK_PIPE */
  1061.  
  1062.  
  1063. #ifdef WANT_STRSTR
  1064.  
  1065. /*
  1066.  * strstr - find first occurrence of wanted in s
  1067.  */
  1068.  
  1069. char *                /* found string, or NULL if none */
  1070. strstr(s, wanted)
  1071. char *s;
  1072. char *wanted;
  1073. {
  1074.     register char *scan;
  1075.     register size_t len;
  1076.     register char firstc;
  1077.     extern int strcmp();
  1078.  
  1079.     /*
  1080.      * The odd placement of the two tests is so "" is findable.
  1081.      * Also, we inline the first char for speed.
  1082.      * The ++ on scan has been moved down for optimization.
  1083.      */
  1084.     firstc = *wanted;
  1085.     len = strlen(wanted);
  1086.     for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
  1087.         if (*scan++ == '\0')
  1088.             return (char *)0;
  1089.     return scan;
  1090. }
  1091. #endif
  1092.  
  1093. #ifdef WANT_FTRUNCATE
  1094.  
  1095. int
  1096. ftruncate(fd, length)
  1097. int fd;
  1098. off_t length;
  1099. {
  1100.     errno = EIO;
  1101.     return -1;
  1102. }
  1103.  
  1104. #endif
  1105.  
  1106.  
  1107.  
  1108.  
  1109. extern FILE *msg_file;
  1110.  
  1111. #ifdef STDC_MSG
  1112. #include <stdarg.h>
  1113.  
  1114. void
  1115. msg(char *str,...)
  1116. {
  1117.     va_list args;
  1118.  
  1119.     va_start(args,str);
  1120.     fflush(msg_file);
  1121.     fprintf(stderr,"%s: ",tar);
  1122.     if(f_sayblock)
  1123.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1124.     vfprintf(stderr,str,args);
  1125.     va_end(args);
  1126.     putc('\n',stderr);
  1127.     fflush(stderr);
  1128. }
  1129.  
  1130. void
  1131. msg_perror(char *str,...)
  1132. {
  1133.     va_list args;
  1134.     int save_e;
  1135.     extern int errno;
  1136.  
  1137.     save_e=errno;
  1138.     fflush(msg_file);
  1139.     fprintf(stderr,"%s: ",tar);
  1140.     if(f_sayblock)
  1141.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1142.     va_start(args,str);
  1143.     vfprintf(stderr,str,args);
  1144.     va_end(args);
  1145.     errno=save_e;
  1146.     perror(" ");
  1147.     fflush(stderr);
  1148. }
  1149. #endif
  1150.  
  1151. #ifdef VARARGS_MSG
  1152. #include <varargs.h>
  1153. void msg(str,va_alist)
  1154. char *str;
  1155. va_dcl
  1156. {
  1157.     va_list args;
  1158.  
  1159.     fflush(msg_file);
  1160.     fprintf(stderr,"%s: ",tar);
  1161.     if(f_sayblock)
  1162.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1163.     va_start(args);
  1164.     vfprintf(stderr,str,args);
  1165.     va_end(args);
  1166.     putc('\n',stderr);
  1167.     fflush(stderr);
  1168. }
  1169.  
  1170. void msg_perror(str,va_alist)
  1171. char *str;
  1172. va_dcl
  1173. {
  1174.     va_list args;
  1175.     int save_e;
  1176.     extern int errno;
  1177.  
  1178.     save_e=errno;
  1179.     fflush(msg_file);
  1180.     fprintf(stderr,"%s: ",tar);
  1181.     if(f_sayblock)
  1182.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1183.     va_start(args);
  1184.     vfprintf(stderr,str,args);
  1185.     va_end(args);
  1186.     errno=save_e;
  1187.     perror(" ");
  1188.     fflush(stderr);
  1189. }
  1190. #endif
  1191.  
  1192. #ifdef DOPRNT_MSG
  1193. void msg(str,args)
  1194. char *str;
  1195. int args;
  1196. {
  1197.     fflush(msg_file);
  1198.     fprintf(stderr,"%s: ",tar);
  1199.     if(f_sayblock)
  1200.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1201.     _doprnt(str, &args, stderr);
  1202.     putc('\n',stderr);
  1203.     fflush(stderr);
  1204. }
  1205.  
  1206. void msg_perror(str,args)
  1207. char *str;
  1208. {
  1209.     int save_e;
  1210.     extern int errno;
  1211.  
  1212.     save_e=errno;
  1213.     fflush(msg_file);
  1214.     fprintf(stderr,"%s: ",tar);
  1215.     if(f_sayblock)
  1216.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1217.     _doprnt(str, &args, stderr);
  1218.     errno=save_e;
  1219.     perror(" ");
  1220.     fflush(stderr);
  1221. }
  1222.  
  1223. #endif
  1224. #ifdef LOSING_MSG
  1225. void msg(str,a1,a2,a3,a4,a5,a6)
  1226. char *str;
  1227. {
  1228.     fflush(msg_file);
  1229.     fprintf(stderr,"%s: ",tar);
  1230.     if(f_sayblock)
  1231.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1232.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1233.     putc('\n',stderr);
  1234.     fflush(stderr);
  1235. }
  1236.  
  1237. void msg_perror(str,a1,a2,a3,a4,a5,a6)
  1238. char *str;
  1239. {
  1240.     int save_e;
  1241.     extern int errno;
  1242.  
  1243.     save_e=errno;
  1244.     fflush(msg_file);
  1245.     fprintf(stderr,"%s: ",tar);
  1246.     if(f_sayblock)
  1247.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1248.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1249.     fprintf(stderr,": ");
  1250.     errno=save_e;
  1251.     perror(" ");
  1252. }
  1253.  
  1254. #endif
  1255.